<template>
	<div v-loading="loading">
		<!-- <div class="breadcrumb">
			<el-button @click="$router.back()" icon="el-icon-arrow-left" type="text" size="medium">返回上一页</el-button>
		</div> -->
		<div class="breadcrumb">
			<el-breadcrumb separator="/">
				<el-breadcrumb-item>{{$store.state.company.companyName}}</el-breadcrumb-item>
				<el-breadcrumb-item>数字果园</el-breadcrumb-item>
				<el-breadcrumb-item :to="{ path:`/digitalOrchard/working/baseList?t=${new Date().getTime()}`}">基地列表</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<div class="tableNavBox">
			<div class="menberConTitle flex" style="justify-content: space-between;">
				<div class="tableTitle">基地基本信息</div>
			</div>
			<formTemplate :formSet="formSet1" :postData="formData1" class="baseadd">
				<template slot="baseKeyValue" slot-scope="scope" >
					<el-form-item label="基地位置：" style="width:500px">
						<div style="width:500px">
							<addressD :value='baseKeyValue' :disabled="isDetail" @getAddressData="getAddressData" ></addressD>
						</div>
						<!-- <el-input v-model="formData1.baseLocation" style="width:500px" :disabled="isDetail"></el-input> -->
					</el-form-item>
				</template>
				<template slot="baseAddress" slot-scope="scope" >
					<el-form-item label="" style="width:500px">
						<el-input v-model="formData1.baseAddress" style="width:500px" :disabled="isDetail" placeholder="请输入详细地址" @change="checkAdd">
							<img slot="suffix" src="@/assets/image/digitalOrchard/dingwei.png" alt="" class="locationBtn" @click="checkMap()"/>
						</el-input>
					</el-form-item>
				</template>
				<template slot="area" slot-scope="scope" >
					<div style="display:flex;justify-content: start">
						<el-form-item label="基地面积：" style="width:300px" label-width="100px">
							<el-input v-model="formData1.area" style="width:200px" :disabled="isDetail">
								<template slot="append">亩</template>
							</el-input>
                    	</el-form-item>
						<el-form-item label="圈定面积：" style="width:260px" label-width="100px">
							<el-input v-model="formData1.fixedArea" style="width:200px" :disabled="true" placeholder="请编辑基地">
								<template slot="append">亩</template>
							</el-input>
                    	</el-form-item>
						<!-- <el-button style="width:60px;height:32px;margin-left:40px" type="primary" @click="selectMap" :disabled="isDetail">圈定</el-button> -->
					</div>
				</template>
			</formTemplate>

			<div class="menberConTitle flex" style="justify-content: space-between;">
				<div class="tableTitle">基地管理信息</div>
			</div>
			<formTemplate :formSet="formSet2" :postData="formData2" :optionData="optionData">
				<template slot="departmentId" slot-scope="scope" >
					<el-form-item label="管理部门：" class="ment">
						<el-cascader no-data-text="请先新建部门" :props="{ checkStrictly: true }" style="width:500px;" v-model="formData2.departmentId" :disabled="isDetail" :options="optionData.departmentId" :show-all-levels="false" @change="getUsersByCompanyId"></el-cascader>
					</el-form-item>	
            	</template>
			</formTemplate>

			<div class="menberConTitle flex" style="justify-content: space-between;">
				<div class="tableTitle">栽培管理</div>
			</div>

			<tableForm :loading="loading" :pageData="formData2.cultivarsEntity" :data="tableSet" :addNew="false" :optionData="optionData" @watchinput="watchinput" @selectChange="selectChange">
				<template slot="rowSpacing" slot-scope="scope">
					<el-input type='number' class="baseaddtable" :disabled="isDetail" v-model.number="scope.row.row.rowSpacing" style="width:45px;" @change="calarea(scope.row.$index)"></el-input>*
					<el-input type='number' class="baseaddtable" :disabled="isDetail" v-model.number="scope.row.row.meterDistance" style="width:45px" @change="calarea(scope.row.$index)"></el-input>
				</template>

				<template slot="varietyName" slot-scope="scope">
					<!-- <el-autocomplete
					  v-model="scope.row.row.varietyName"
					  :disabled="isDetail"
					  :fetch-suggestions="querySearchAsync"
					  placeholder="请输入内容"
					  @blur="querySearchRow(scope.row.$index)"
					  @select="handleSelect"
					></el-autocomplete> -->
					<el-cascader
					v-model="scope.row.row.varietyId"
					:disabled="isDetail"
    				placeholder="请输入搜索"
    				:options="optionData.varietiesId"
					@blur="querySearchRow(scope.row.$index)"
					@change="getCheckedNodes(scope.row.$index)"
    				filterable>
					</el-cascader>
				</template>

				<template slot="m" slot-scope="scope">
					<div style="display:flex; justify-content:space-around;font-weight:bold;">
						<!-- <p  style="color:#67C23A;cursor: pointer;" @click="selectMapTable(scope.row.$index)">圈定</p> -->
						<p  style="color:red;cursor: pointer;" @click="delMapTable(scope.row,scope.row.$index)">删除</p>
					</div>
				</template>
			</tableForm>

			<el-button type="primary" v-if="isDetail == false" @click="formData2.cultivarsEntity.items.push({})" style="margin:10px 0 0 10px; ">增加品种</el-button>
			<div style="clear:both"></div>
			
			<!-- 地图 -->
			<div class="mapBox">
				<!-- <BIGENAP ref="BIGENAP" :variety="variety" id="base" :baseId="formData1.id" :baseName="formData1.baseName" :baseType="formData1.id?'':'noBaseId'" :configs="mapConfig" @cacheData="cacheData" :key="bigemapKey"></BIGENAP> -->
				<mars3d ref="mars3ds" :lngLat="baseLatLng" :isAdd="isAdd" :linkBtn="linkBtn" :baseId="cacheId" @cacheData="cacheData" :key="bigemapKey"></mars3d>
			</div>
			<div class="btnBox" v-if="isDetail == false">
				<el-button type="primary" round @click="submitData">保存</el-button>
				<!-- <el-button type="danger" round>删除</el-button> -->
			</div>
			<div style="clear:both"></div>
		</div>
	</div>
</template>

<script>
    import formTemplate from '@/components/form.vue'
	import tableForm from '@/components/tableForm.vue'
	import BIGENAP from '@/components/newMap.vue'
	import addressD from '@/components/address.vue'
import { loadingFunc } from '../../../../../components/mapComponents/utils/map'
	window.addEventListener('message', function(event) {
	                // event.data
					console.log(event)
	            })
	export default {
		components: {
            formTemplate,
			BIGENAP,
			addressD,
			tableForm
		},
		data() {
			return {
				variety:{
					varietyId: '',
					varietyIndex: ''
				},
				baseKeyValue:[],
				chooseRow:'',
				mapConfig:{
					isOpenEditor: false, // 是否打开编辑
					isShowEditorBtn: true,
					maxZoom: 17,
					mapUrl: '',
					mapSource: 'bigemap.arcgis-satellite',
					// coverage: ['bigemap.amap-streets']
				},
                formSet1:[
                    { span: 24, label: '基地名称：', prop: 'baseName', type: 'input', disabled:false , placeholder:"请输入",labelWidth:'500px'},
                    { span: 24, label: '基地位置：', prop: 'baseKeyValue', type: 'slot' , disabled:false},
                    { span: 24, label: '', prop: 'baseAddress', type: 'slot' , disabled:false},
                    { span: 24, label: '基地面积：', prop: 'area', type: 'slot',},
                ],
				formSet2:[
                    { span: 13, label: '管理部门：', prop: 'departmentId', type: 'slot', disabled:false },
                    { span: 13, label: '联系人：', prop: 'contacts', type: 'select' , disabled:false, placeholder:"请先选择部门" },
                ],
				tableSet:[
					{ label: '编号', prop: 'tableId', type: 'index', disabled:false, width:'50px'},
					{ label: '品种', prop: 'varietyName', type: 'slot', disabled:false,labelwidth:'140%' , width:'220px'},
					{ label: '株行距/米', prop: 'rowSpacing', type: 'slot', disabled:false},
					{ label: '亩栽培/株', prop: 'muCultivation', type: 'input' , disabled:true },
					{ label: '圈定面积（亩）', prop: 'enclosureArea', type: 'input' , disabled:true, placeholder:"请编辑基地"},
					{ label: '总株数', prop: 'total', type: 'input', disabled:true },
					{ label: '栽培年份', prop: 'cultivationYear', type: 'year' , disabled:false},
					// { label: '标识', prop: 'colorMark', type: 'color' , value:'暂无', width:'50%'},
					{ label: '操作', prop: 'm', type: 'slot' , width:'50px'},
				],
				optionData:{
					departmentId:[],
					contacts:[],
					varietiesId:[]
				},
                formData1:{
					baseLocation:'',
					fixedArea:''
				},
				formData2:{
					cultivarsEntity:{
						items:[],
					}
				},
				companyId: this.$store.state.company.companyId,
				companyName: this.$store.state.company.companyName,
				postData: {
					baseName: '', // 公司ID
					baseLocation: '',
					latitude:'',
					departmentId:'',
					contacts:'',

				},
				isDetail:false, // 是否为详情页
				isOrchard:false, // 是否为修改页
				bigemapKey: new Date().getTime(), // 地图刷新
				cacheId:'', // 临时基地id
				isvariety:false,
				loading:false,//表格加载
				Vname:'', //选中品类
				niceJump:false, //保存跳转
				linkBtn:true, //编辑地图
				baseLatLng:[], //经纬度
				isAdd:true, //是否为新增
				cacheTable:[],//暂存表格数据
			}
		},
		created() {
			this.$store.commit('industrialis_leftNavIndex', 21)
			this.getDepartment()
			this.queryVarieties()
			if(this.$route.query.orchardID){
				const editorMapData = JSON.parse(localStorage.getItem('editorMapData'))
				if(editorMapData == null){
					this.loading = true
					this.getOrchardDetailsByID(this.$route.query.orchardID)
				}else{
					this.isOrchard = true
				}
				this.isAdd = false
			}else if(this.$route.query.detailID){
				this.loading = true
				this.getOrchardDetailsByID(this.$route.query.detailID)
				this.formSet1.forEach(item =>{
					item.disabled = true
				})
				this.formSet2.forEach(item =>{
					item.disabled = true
				})
				this.tableSet.forEach((item,index) =>{
					item.disabled = true
					if(item.label == '操作'){
						this.tableSet.splice(index,1)
					}
				})
				this.isDetail = true
				this.linkBtn = false
				this.mapConfig.isShowEditorBtn = false //只在created可控制
				this.isAdd = false
			}
			this.mapNewKey = new Date().getTime()
		},
		mounted() {
			this.getCache()
			setTimeout(() => {
				this.getAllAreaByFolderId()
				if(this.isDetail || this.$route.query.orchardID){
					if(this.formData2.cultivarsEntity.items.length >0){
						this.getAllAreaByFolderId()
					}
				}
			}, 1900);
			// window.addEventListener('beforeunload', e => this.beforeunloadHandler(e))
		},
		beforeRouteLeave(to, from, next){
			console.log('离开新增')
			if(to.path != '/editormap' && this.niceJump == false){
				this.deleteTemporaryBase()
			}else if(to.path == '/editormap'){
				this.cacheData()
			}
			next()
		},
		beforeRouteEnter(to, from, next) {
		    next(vm => {
		    //   sessionStorage.setItem("lasterRouter", from.path)
		    })
		  },
		wathc:{
		},
		computed: {
		},
		methods: {
			getCache(){
				//读取缓存
				const editorMapData = JSON.parse(localStorage.getItem('editorMapData'))
				console.log(editorMapData,'editorMapData');
				console.log(this.$route.query.orchardID,'id');
				if(editorMapData != null){
					if(editorMapData.postData){
						if(editorMapData.postData.baseKeyValue){
							this.baseKeyValue = editorMapData.postData.baseKeyValue.split('/')
						}
						this.formData1 = editorMapData.postData
						this.formData2 = editorMapData.postData
					}
					if(editorMapData.contacts){
						this.optionData.contacts = editorMapData.contacts
					}
					this.cacheId = editorMapData.cacheId
					this.baseLatLng = editorMapData.lnglat
					this.bigemapKey = new Date().getTime()
					localStorage.removeItem('editorMapData')

				}else if(this.$route.query.orchardID == undefined && this.$route.query.detailID == undefined){
					this.getCacheId()
				}
			},
			checkAdd(){ // 是否有重复地址
				this.formData1.baseLocation = this.formData1.baseTextValue+this.formData1.baseAddress
				this.$request.get(this.$api.baseIsDuplicate, {
						baseLocation: this.formData1.baseLocation,
						orchardId: this.$route.query.orchardID?this.$route.query.orchardID:this.cacheId
					}).then(res=>{
					}).catch(e =>{
						if(e.code == 40035){
							this.formData1.baseAddress = ''
						}
					})
			},
			deleteTemporaryBase(){ //删除后台文件夹缓存
				localStorage.removeItem('editorMapData')
				// if(this.isDetail == false && this.isOrchard == false){
				// 	this.$request.postForm(this.$api.deleteTemporaryBase, {
				// 		baseId: this.cacheId
				// 	}).then(res=>{})
				// }
			},
			//缓存
			cacheData(){
				try{
					let demo = {}
					demo.postData = Object.assign(this.formData1,this.formData2)
					demo.contacts = this.optionData.contacts
					demo.cacheId = this.cacheId
					demo.lnglat = this.baseLatLng
					console.log(demo,'demo')
					localStorage.setItem('editorMapData', JSON.stringify(demo))
				}catch(e){
					this.$message.error('缓存数据保存失败')
				}
			},
			getCacheId(){ //获取临时基地id
				this.$request.postForm(this.$api.getTheIdOfTheTemporaryBase).then(res=>{
						if(res.code == 200){
							console.log(res.data);
							this.cacheId = res.data
						}
					})
			},
			getAllAreaByFolderId(){ //获取表格圈地面积
				this.$request.postForm(this.$api.getAllAreaByFolderId, {
					baseId:this.cacheId,
				}).then(res=>{
						if(res.code == 200){
							let b = 0
							let isFly = false
							let fly = 0
							console.log(this.formData2.cultivarsEntity.items,'getAllAreaByFolderId');
							res.data.forEach((item,index) =>{
								this.formData2.cultivarsEntity.items.forEach((val,i) =>{
									if(item.folderId == val.folderId){
										let a = parseFloat(item.area)
										a = a * 0.0015
										this.$set(this.formData2.cultivarsEntity.items[i], 'enclosureArea', a.toFixed(2))
									}else{
										item.flag = true
									}
									this.watchinput({prop:0, index:i})
								})
								b += parseFloat(item.area)
								if(item.latitude && item.longitude){
									isFly = true
									fly = index
								}
							})
							if(isFly == true){
								console.log(res.data[fly],'fly');
								this.baseLatLng[0] = res.data[fly].longitude
								this.baseLatLng[1] = res.data[fly].latitude
							}else{
								this.baseLatLng = []
							}
							this.checkMap()
								// 是否在地图内删除文件夹
							this.formData2.cultivarsEntity.items.forEach((item,index) =>{
								if(item.flag){
									this.formData2.cultivarsEntity.items.splice(index,1)
								}
							})
							b = b * 0.0015
							this.formData1.fixedArea = b.toFixed(2)
						}
					})
			},
			getCheckedNodes(e){ // 处理品类级联
				this.chooseRow = e
				let b = this.formData2.cultivarsEntity.items[e].varietyId.length-1
				console.log(this.formData2.cultivarsEntity.items,'idddddd');
				let flag = true
				this.formData2.cultivarsEntity.items.forEach((item,index) =>{
					if(item.varietyId[item.varietyId.length-1] == this.formData2.cultivarsEntity.items[e].varietyId[b] && index != e){
						this.$message.error('请勿添加重复品种')
						this.formData2.cultivarsEntity.items[e].varietyId = ''
						flag = false
					}
				})
				if(flag){
					this.returnVarietiesId(this.formData2.cultivarsEntity.items[e].varietyId[b], this.optionData.varietiesId)
					console.log(this.Vname);
					this.formData2.cultivarsEntity.items[e].varietyName = this.Vname
					this.addTemporaryStorageVarieties()
				}
			},
			querySearchRow(e){
				// this.chooseRow = e
				
				// // this.formData2.cultivarsEntity.items[e].varietyName = this.returnVarietiesId(this.formData2.cultivarsEntity.items[e].varietyId, this.optionData.varietiesId[b])
				// // console.log(this.formData2.cultivarsEntity.items[e]);
				// setTimeout(() => {
				// 	if(this.isvariety == false){
				// 		this.addTemporaryStorageVarieties()
				// 	}else{
				// 		this.isvariety = false
				// 	}
				// }, 500);
			},
		
			returnVarietiesId(e,list){ // 品类递归
				list.forEach((item,index) =>{
					if(e == item.value){
						this.Vname =  item.label
						console.log(this.Vname);
						return;
					}
					if(e != item.value && item.children){
						this.returnVarietiesId(e, item.children)
					}
				})
			
			},
			querySearchAsync(e,cb){ //品种 模糊搜索
				if(e){
					this.$request.postForm(this.$api.fuzzyQueryVarieties, {
						keyWord:e
					}).then(res=>{
						if(res.code == 200){
							let val = res.data.map( item =>{
								return{
									value:item.varietyName,
									id:item.id
								}
							})
							setTimeout(() => {
								cb(val)
							}, 3000 * Math.random());
						}
					})
				}
			},
			handleSelect(e){// 选中
				this.isvariety = true
				this.formData2.cultivarsEntity.items[this.chooseRow].varietyName = e.value
				this.formData2.cultivarsEntity.items[this.chooseRow].varietyId = e.id
				this.addTemporaryStorageVarieties()
			},
			addTemporaryStorageVarieties(){ // 新增地图文件夹接口
				this.loading = true
				console.log(this.formData2);
				console.log(this.chooseRow);
				let folderId = this.formData2.cultivarsEntity.items[this.chooseRow].folderId
				this.$request.postForm(this.$api.addTemporaryStorageVarieties, {
						folderId:folderId?folderId:null,
						temporaryBaseId:this.cacheId,
						varietiesName:this.formData2.cultivarsEntity.items[this.chooseRow].varietyName
					}).then(res=>{
						if(res.code == 200){
							this.formData2.cultivarsEntity.items[this.chooseRow].folderId = res.data.folderId
						}
						this.loading = false
					})
			},
			selectMapTable(e){
				this.variety.varietyId = this.formData2.cultivarsEntity.items[e].varietiesId
				this.variety.varietyIndex = e
				console.log(this.variety);
				this.$refs.BIGENAP.toEditorPages()
			},
			delMapTable(e,index){
				if(this.formData2.cultivarsEntity.items[index].varietyName){
					this.$request.postForm(this.$api.deleteTheTemporarilyStoredVarietyL, {
						folderId:this.formData2.cultivarsEntity.items[index].folderId,
					}).then(res=>{
						if(res.code == 200){
							this.formData2.cultivarsEntity.items.splice(index,1)
							console.log(this.formData2);
						}
					})
				}else{
					this.formData2.cultivarsEntity.items.splice(index,1)
				}
			},
			selectChange(e){
				if(e.prop == "varietiesId"){
					console.log(this.optionData.varietiesId);
					this.$refs.BIGENAP.getAreas((res)=>{
						console.log(res);
						if(res.areaList){
							let isFlag = false
							let flagData = ''
							res.areaList.forEach(item =>{
								if(item.varietyId == e.id){
									isFlag = true
									flagData = item.area
								}
							})
							if(isFlag == true){
								this.formData2.cultivarsEntity.items[e.index].enclosureArea = flagData
							}else{
								this.formData2.cultivarsEntity.items[e.index].enclosureArea = ''
							}
						}
    				})
				}
			},
			watchinput(e){//表格监听
				//prop === 0时为株行距/米改变重新计算
				if(e.prop == 'muCultivation' || e.prop == 'enclosureArea' || e.prop == '0'){
					if(this.formData2.cultivarsEntity.items[e.index].muCultivation && this.formData2.cultivarsEntity.items[e.index].enclosureArea){
						let sum = (this.formData2.cultivarsEntity.items[e.index].muCultivation * this.formData2.cultivarsEntity.items[e.index].enclosureArea).toFixed(0)
						this.$set(this.formData2.cultivarsEntity.items[e.index], 'total', sum)
					}
				}
			},
			calarea(index){
				if(this.formData2.cultivarsEntity.items[index].rowSpacing && this.formData2.cultivarsEntity.items[index].meterDistance){
					console.log(this.formData2.cultivarsEntity.items[index].rowSpacing);
					let a = Number(this.formData2.cultivarsEntity.items[index].rowSpacing).toFixed(1)
					let b = Number(this.formData2.cultivarsEntity.items[index].meterDistance).toFixed(1)
					let e = 666/(a * b)
					e = e.toFixed(0)
					this.$set(this.formData2.cultivarsEntity.items[index], 'muCultivation', e)
					this.watchinput({prop:'0',index:index})
				}
			},
			checkMap(){
				// 飞图---------到指定位置
				/**
				 * @param {latLng}  坐标 object {lat: 28.92163128242129, lng: 111.70898437500001} or Arry[28.92163128242129,111.70898437500001].
				 * @param {zoom}  显示级别 number
				 */
				if(this.formData1.baseTextValue != ''){
					if(this.baseLatLng.length == 0){
						this.$request.postForm(this.$api.queryGeoCoding, {
							address: this.formData1.baseTextValue,
						}).then(res=>{
							if(res.code == 200){
								this.baseLatLng[0] = res.data.longitude
								this.baseLatLng[1] = res.data.latitude
								this.$refs.mars3ds.flyTo(this.baseLatLng)
							}
						})
					}else{
						this.$refs.mars3ds.flyTo(this.baseLatLng)
					}
				}
			},
			//获取总圈定数据
			selectMap(){
				// this.$refs.BIGENAP.toEditorPages()
			},
			getAddressData(e){
				// let num = e.key.split('/')
				// let arr = e.text.split('/')
				// num.forEach((item,index) =>{
				// 	const key = item
				// 	let arr2 = {}
				// 	arr2[key] = arr[index]
				// 	arr[index] = arr2
				// })
				console.log(e);
				let reg = new RegExp('/','g')
				let arr = e.text.replace(reg,'')
				this.formData1.baseTextValue = arr
				console.log(this.formData1.baseTextValue);
				this.formData1.baseKeyValue = e.key
				this.checkMap()
				// this.formData1.baseTextValue = e.text
			},
			getUsersByCompanyId(e){
				this.$request.postForm(this.$api.getUsersByDepartmentId, {
					departmentId: e[e.length-1]
				}).then(res=>{
					if(res.code == 200){
						this.optionData.contacts = res.data
						console.log(this.optionData,'optionData');
						// this.formData2.contacts = this.optionData.contacts[0].id
						this.formData2.departmentId = e[e.length-1]
						// this.formData2.contacts =  res.data[0]
					}
				})
			},
			queryVarieties(){ // 获取品类接口
				this.$request.get(this.$api.queryVarieties).then(res=>{
					if(res.code == 200){
						this.optionData.varietiesId = this.removeChild(res.data) 
					}
				})
			},
			removeChild(data) {
            // 树形递归删除最后children
        	    data.forEach((item, index) => {
        	        if (item.children && item.children.length != 0) {
        	            this.removeChild(item.children)
        	        } else {
        	            delete item.children
        	        }
        	    })
        	    return data
        	},
			getDepartment(){
				if(this.$store.state.company.companyId){
					this.$request.postForm(this.$api.getDepartmentByCompanyId, {
					companyId: this.$store.state.company.companyId,
					}).then(res=>{
						if(res.code == 200){
							this.optionData.departmentId = this.mapTree(res.data)
							console.log(this.optionData.departmentId);
						}
					})
				}
			},
			mapTree(e){
				e.forEach(item =>{
					if(item.children.length != 0){
						item.label = item.departmentName
						item.value = item.id
						item.children = this.mapTree(item.children)
					}else{
						item.label = item.departmentName
						item.value = item.id
					}
				})
				return e
			},
			getOrchardDetailsByID(e){
				this.$request.postForm(this.$api.getOrchardDetailsByID, {
					orchardID: e,
				}).then(res=>{	
					if(res.code == 200){
						// res.data.basicInfo.baseLocation = res.data.basicInfo.baseKeyValue
						
						this.formData1 = res.data.basicInfo
						this.cacheId = this.formData1.id
						this.baseKeyValue = res.data.basicInfo.baseKeyValue.split('/')
						res.data.basicInfo.cultivarsEntity = {}
						res.data.basicInfo.cultivarsEntity.items = []
						res.data.basicInfo.cultivarsEntity.items = res.data.plantingInformation

						this.formData2 = res.data.basicInfo	
						this.formData2.cultivarsEntity.items.forEach(item =>{
							if(item.varietyId){
								item.varietyId = item.varietyId.split('/')
							}
						})
						this.optionData.contacts = [{name:res.data.basicInfo.contactsName, value:res.data.basicInfo.contacts}]
						
						// if(this.$route.query.orchardID){
						// 	this.getAllAreaByFolderId()
						// }
						setTimeout(() => {
							this.loading = false
						}, 1600);
						console.log(this.formData1,'formData2');
						this.checkMap()
					}
				})
			},
			submitData() {
				// // 保存/修改
				this.cacheTable = this.formData2.cultivarsEntity.items
				this.formData2.cultivarsEntity.items.forEach(item =>{
					if(item.varietyId){
						item.varietyId = item.varietyId.join('/')
					}
				})
				this.postData = Object.assign(this.formData1,this.formData2)
				// this.$refs.BIGENAP.getMapCoverageData(res =>{
				// 	if(res && res.length > 0){
				// 		this.postData.content = {}
				// 		this.postData.content.json = res
				// 		this.postData.content.json.forEach((item,index) =>{
				// 			item.tableId = index+1
				// 		})
				// 		console.log(this.postData.content,'content');
				// 		this.postData.content = JSON.stringify(this.postData.content) 
				// 	}
				// })
				// console.log(this.postData);
				if(!this.postData.baseName){
					this.$message.error('请填写基地名称')
					return
				} else if(!this.postData.baseTextValue){
					this.$message.error('请填写基地位置')
					return
				} else if(!this.postData.area){
					this.$message.error('请填写基地面积')
					return
				} else if(!this.postData.departmentId){
					this.$message.error('请选择管理部门')
					return
				} else if(!this.postData.contacts){
					this.$message.error('请选择联系人')
					return
				}
				else if(!this.postData.fixedArea){
					this.$message.error('请填写圈地面积')
					return
				}
				this.postData.baseTemporaryId = this.cacheId
				this.postData.companyId = this.companyId
				// this.postData.cultivarsEntity = JSON.stringify(this.postData.cultivarsEntity) 
				console.log(this.postData);
				if(this.postData.id){
					this.postData.orchardId = this.postData.id
					delete this.postData.id
					this.$request.post(this.$api.updateOrchard, this.postData).then(res=>{
						if(res.code == 200){
							this.$message.success('保存成功')
							this.niceJump = true
							this.$router.push(`/digitalOrchard/working/baseList?t=${new Date().getTime()}`)
						}
					})
				}else{
					this.$request.post(this.$api.addOrchard, this.postData).then(res=>{
				    	if(res.code == 200){
				    		this.$message.success('保存成功')
							this.niceJump = true
				    		this.$router.push(`/digitalOrchard/working/baseList?t=${new Date().getTime()}`)
				    	}
				    })
				}
			},
			getCompanyMenbers() {
				// 获取公司成员 
				this.$request.postForm(this.$api.getUsersByCompanyId, {
					companyId: this.companyId,
					page: 1,
					pageSize: 20000
				}).then(res => {
					if (res.code == 200) {
						this.menbersList = res.data.items
						// 编辑获取数据
						if(this.$route.query.id){
							setTimeout(()=>{
								this.getClassDetail()
							},300)
						}
					}
				})
			},
		}
	}
</script>

<style>
    /* 取消input框为number时的上下选择 */
	input::-webkit-outer-spin-button, 
    input::-webkit-inner-spin-button {
        -webkit-appearance: none;
    }
    input[type="number"]{
        -moz-appearance: textfield;
    }
	/* 地址选择器 */
	.address_cascader .el-cascader-menu__wrap { 
		height: 300px !important;
	}
	
</style>

<style scoped="scoped">	
	.baseaddtable >>> .el-input__inner{
		padding: 0 5px !important;
	}
	.breadcrumb,
	.tableNavBox {
		padding: 20px;
	}

	.tableNavBox {
		padding-top: 0;
	}

	.tableTitle {
		font-size: 16px;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #333333;
		line-height: 30px;
	}

	.menberConTitle {
		width: 100%;
		background: #F3F3F3;
		text-align: left;
		padding: 5px 20px;
		margin-bottom: 20px;
		/* margin-top: 20px; */
	}

	.topButton {
		width: 78px;
	}

	.dialogVisible>>>.el-input__inner {
		width: 270px;
	}

	.demo-form-inline {
		color: #5C5C5C;
	}

	.formInput {
		width: 500px;
	}

	.formInputNub {
		width: 100px;
		margin-right: 10px;
	}

	.formInputDatePicker {
		width: 440px;
		margin-right: 10px;
	}
	.dayNub {
		margin: 0 5px;
	}

	.btnBox {
		width: 100%;
		padding-top: 15px;
		border-top: 1px solid #DCDCDC;
		text-align: center;
	}

	.btnBox button {
		width: 100px;
	}
	.expertTag{
		margin-right: 10px;
		margin-bottom: 10px;
	}
	.mapBox{
		/* width: 100%; */
		width: calc(100% - 20px);
		margin-left: 10px;
		height: 100%;
		margin-top: 40px;
		/* margin-bottom: 30px; */
	}
	.locationBtn{
		width: 18px;
		height: 18px;
		position: relative;
		top: 7px;
		cursor: pointer;
		
	}
</style>
